﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Salon
{
    class OrderService
    {
        public OrderService() { }

        public Order saveOrder(Order order)
        {
            if (order.Id < 1)
            // NOWE
            {
                String insertQuery = "INSERT INTO Orders VALUES(@salesman_id,@client_id,@order_date,@pay_date,@end_date,@price,@cost,@state,@salon,@end_price)  SELECT SCOPE_IDENTITY() ";

                SqlCommand cmd = SqlService.getInstance().getCommand( insertQuery );
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@salesman_id", order.Salesman.Id);
                cmd.Parameters.AddWithValue("@client_id", order.Client.Id);
                cmd.Parameters.AddWithValue("@order_date", order.OrderDate);
                cmd.Parameters.AddWithValue("@pay_date", order.PayDate);
                cmd.Parameters.AddWithValue("@end_date", order.EndDate);
                cmd.Parameters.AddWithValue("@price", order.Price);
                cmd.Parameters.AddWithValue("@cost", order.Cost);
                cmd.Parameters.AddWithValue("@state", order.State);
                cmd.Parameters.AddWithValue("@salon", order.SalonId);
                cmd.Parameters.AddWithValue("@end_price", order.EndPrice);

                order.Id = Convert.ToInt32( cmd.ExecuteScalar() ); // zwraca wstawiony obiekt z id nadanym przez SQL
            }
            else
            // UPDATE
            {
                String updateQuery = "UPDATE Orders SET salesman_id=@salesman_id,client_id=@client_id,order_date=@order_date,pay_date=@pay_date,end_date=@end_date,cost=@cost,price=@price,end_price=@end_price,state=@state WHERE id=@id";
                
                SqlCommand cmd = SqlService.getInstance().getCommand( updateQuery );
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@salesman_id", order.Salesman.Id);
                cmd.Parameters.AddWithValue("@client_id", order.Client.Id);
                cmd.Parameters.AddWithValue("@order_date", order.OrderDate);
                cmd.Parameters.AddWithValue("@pay_date", order.PayDate);
                cmd.Parameters.AddWithValue("@end_date", order.EndDate);
                cmd.Parameters.AddWithValue("@price", order.Price);
                cmd.Parameters.AddWithValue("@cost", order.Cost);
                cmd.Parameters.AddWithValue("@state", order.State);
                cmd.Parameters.AddWithValue("@id", order.Id);
                cmd.Parameters.AddWithValue("@salon", order.SalonId);
                cmd.Parameters.AddWithValue("@end_price", order.EndPrice);
                cmd.ExecuteNonQuery();
            
            }

            return order;
        }
        public Order getOrderById(int id)
        {
            String query = "SELECT * FROM Orders WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader rdr = cmd.ExecuteReader();
            if (!rdr.HasRows)
                return null;
            rdr.Read();

            Order order = new Order();
            order.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
            order.Salesman = ServiceFactory.getUserService().getUserById(rdr.GetInt32(rdr.GetOrdinal("salesman_id")));
            order.Client = ServiceFactory.getClientService().getClientById(rdr.GetInt32(rdr.GetOrdinal("client_id")));
            order.OrderDate = rdr.GetDateTime(rdr.GetOrdinal("order_date"));
            order.PayDate = rdr.GetDateTime(rdr.GetOrdinal("pay_date"));
            order.EndDate = rdr.GetDateTime(rdr.GetOrdinal("end_date"));
            order.Price = rdr.GetDecimal(rdr.GetOrdinal("price"));
            order.Cost = rdr.GetDecimal(rdr.GetOrdinal("cost"));
            order.State = rdr.GetInt32(rdr.GetOrdinal("state"));
            order.EndPrice = rdr.GetDecimal(rdr.GetOrdinal("end_price"));
            order.SalonId = rdr.GetInt32(rdr.GetOrdinal("salon_id"));

            return order;
        }

        public IList<Order> getOrders(IList<IFilter> filterList)
        {

            IList<Order> Orders = new List<Order>();
            String query = "SELECT * FROM Orders WHERE id > 0 ";
            if(filterList!=null) query += Global.getConditionString(filterList);

            SqlDataReader rdr = SqlService.getInstance().getCommand(query).ExecuteReader();
            if (rdr.HasRows)
            {
                while (rdr.Read())
                {
                    Order order = new Order();
                    order.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
                    order.Salesman = ServiceFactory.getUserService().getUserById(rdr.GetInt32(rdr.GetOrdinal("salesman_id")));
                    order.Client = ServiceFactory.getClientService().getClientById(rdr.GetInt32(rdr.GetOrdinal("client_id")));
                    order.OrderDate = rdr.GetDateTime(rdr.GetOrdinal("order_date"));
                    order.PayDate = rdr.GetDateTime(rdr.GetOrdinal("pay_date"));
                    order.EndDate = rdr.GetDateTime(rdr.GetOrdinal("end_date"));
                    order.Price = (decimal)rdr.GetFloat(rdr.GetOrdinal("price"));
                    order.Cost = (decimal)rdr.GetFloat(rdr.GetOrdinal("cost"));
                    order.State = rdr.GetInt32(rdr.GetOrdinal("state"));
                    order.EndPrice = (decimal)rdr.GetFloat(rdr.GetOrdinal("end_price"));
                    order.SalonId = rdr.GetInt32(rdr.GetOrdinal("salon"));

                    Orders.Add(order);
                }
            }
            rdr.Close();
            return Orders;
        }

        //przy anulowaniu ustawiamy stan na anulowane
        public void cancelOrder(Order order)
        {
            String query = "UPDATE Orders SET state = 6 WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", order.Id);
            cmd.ExecuteNonQuery();
        }
        // usunięcie z bazy ???
        public void deleteOrder(Order order)
        {
            String query = "DELETE FROM Orders WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", order.Id);
            cmd.ExecuteNonQuery();
        }
    }
}
